﻿using System;
using System.Collections;
using System.Collections.Generic;
using AST.Model;
using System.Web;

namespace AST.BLL
{
    /// <summary>
    /// 用于读、写、删除、比较Cache中的用户信息。
    /// </summary>
    [Serializable]
    public class CacheBox
    {
        #region 用户Cache操作

        /// <summary>
        /// 檢測User Cache是否存在
        /// </summary>
        /// <returns></returns>
        public static bool CheckUserCache()
        {
            AdminEntity result = null;
            if (HttpContext.Current.Session != null)
            {
                if (HttpContext.Current.Session["User_"] != null)
                {
                    result = HttpContext.Current.Session["User_"] as AdminEntity;
                }
            }

            return result != null;
        }

        /// <summary>
        /// 登记User Cache
        /// </summary>
        /// <param name="userinfo"></param>
        public static void CreateUser(AdminEntity userinfo)
        {
            if (!CheckUserCache())
            {
                HttpContext.Current.Session["User_"] = userinfo;
            }
        }

        /// <summary>
        /// 获取User Cache
        /// </summary>
        /// <returns></returns>
        public static AdminEntity GetUser()
        {
            AdminEntity result = null;
            if (HttpContext.Current.Session != null)
            {
                if (HttpContext.Current.Session["User_"] != null)
                {
                    result = HttpContext.Current.Session["User_"] as AdminEntity;
                }
            }
            return result;
        }

        /// <summary>
        /// 移除User Cache
        /// </summary>
        public static void RemoveUser()
        {
            if (CheckUserCache())
            {
                HttpContext.Current.Session.Remove("User_");
            }
        }

        #endregion
    }
}
